package com.example.gitpart03;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class Register extends AppCompatActivity {

    private CheckBox checkBox;
    private Button registerButton;
    private EditText phoneEditText;
    private EditText codeEditText;
    private DatabaseHelper databaseHelper;
    private TextView sendCodeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yogurt_register_choose);

        checkBox = findViewById(R.id.checkbox_id2);
        registerButton = findViewById(R.id.register_button_id);
        phoneEditText = findViewById(R.id.account);
        codeEditText = findViewById(R.id.password_yanzheng);
        databaseHelper = new DatabaseHelper(this);
        sendCodeText = findViewById(R.id.send_code_text);

        sendCodeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCodeText.setText("已发送");
                sendCodeText.setBackgroundColor(getResources().getColor(android.R.color.white));
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkBox.isChecked()) {
                    Toast.makeText(Register.this, "请您勾选协议", Toast.LENGTH_SHORT).show();
                } else {
                    String phone = phoneEditText.getText().toString().trim();
                    String code = codeEditText.getText().toString().trim();

                    if (phone.isEmpty() || code.isEmpty()) {
                        Toast.makeText(Register.this, "账号和验证码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        registerUser(phone, code);
                        Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            }
        });
    }

    private void registerUser(String phone, String code) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_PHONE, phone);
        values.put(DatabaseHelper.COLUMN_CODE, code);
        db.insert(DatabaseHelper.TABLE_NAME, null, values);
        db.close();
    }
}